package com.web;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
@RestController
public class Upload {
    //还要加个返回 responseBody
    //因为multipartfile 不能得到他的路径，所以需要生成临时文件再 转换 Object类型 再上传
    @RequestMapping(value  =  "/fileUpload",  method  =  RequestMethod.POST)
    @ResponseBody
    public  Object  uploadResource(HttpServletRequest  request)  throws  Exception  {
        MultipartHttpServletRequest  multipartRequest  =  (MultipartHttpServletRequest)  request;
        //相当于获取form表单隐藏域的参数
        String  resourceId  =  "123";
        //获取form表单要上传的文件
        MultipartFile  file  =  multipartRequest.getFile("file");
        //获取文件的文件名字(后面要用到)
        String  filename  =  file.getOriginalFilename();
        //这个url是要上传到另一个服务器上接口
        String  url  =  String.format("http://wgy.vaiwan.com/files/videos/");
        Object  object  =  null;
        //创建HttpClients实体类
        CloseableHttpClient  aDefault  =  HttpClients.createDefault();
        try  {
            HttpPost  httpPost  =  new  HttpPost(url);
            MultipartEntityBuilder  builder  =  MultipartEntityBuilder.create();
            //这里就是自己踩的坑,需要使用file.getBytes()
            //builder.addBinaryBody("file",file.getInputStream(),ContentType.create("multipart/form-data"),filename);
            //使用这个，另一个服务就可以接收到这个file文件了
            builder.addBinaryBody("file",file.getBytes(),ContentType.create("multipart/form-data"),filename);
            StringBody  stringBody  =  new  StringBody(resourceId,ContentType.MULTIPART_FORM_DATA);
            builder.addPart("resource_id",stringBody);
            HttpEntity entity  =  builder.build();
            httpPost.setEntity(entity);

            ResponseHandler<Object>  rh  =  new  ResponseHandler<Object>()  {
                @Override
                public  Object  handleResponse(HttpResponse  response)  throws  IOException  {
                    HttpEntity  entity  =  response.getEntity();
                    String  result  =  EntityUtils.toString(entity,  "UTF-8");
                    return  result;
                }
            };
            aDefault  =  HttpClients.createDefault();
            object  =  aDefault.execute(httpPost,  rh);
        }  catch  (Exception  e)  {
           e.printStackTrace();
        }  finally  {
            aDefault.close();
        }
        return  object;

    }
}
